//
//  WithdrawHistoryViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/16.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
import CRRefresh

class WithdrawHistoryViewController: BaseViewController {

    @IBOutlet var withdrawHistroyTableview: UITableView!
    
    var withdraws :[WithdrawModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configView()
        page = 1
        requstwithdrawHistoryList(page: page!)
    }
    
    
    func configView() -> Void {
        withdrawHistroyTableview.delegate = self
        withdrawHistroyTableview.dataSource = self
        withdrawHistroyTableview.separatorStyle = .none
        withdrawHistroyTableview.showsVerticalScrollIndicator = false
        withdrawHistroyTableview.backgroundColor = BaseColor.BackGroundColor
        withdrawHistroyTableview.register(UINib(nibName: "WithdrawHistoryTableViewCell", bundle: nil), forCellReuseIdentifier: "historyCell")
        withdrawHistroyTableview.cr.addHeadRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol) { [weak self] in
            page = 1
            self?.requstwithdrawHistoryList(page: page!)
        }
        withdrawHistroyTableview.cr.addFootRefresh(animator: SlackLoadingAnimator() as CRRefreshProtocol)  { [weak self] in
            page! += 1
            self?.requstwithdrawHistoryList(page: page!)
        }
    }
    
    //网络请求
    func requstwithdrawHistoryList(page:Int) -> Void {
        
        Alamofire.request(getWithdrawHistoryUrl, method:.post, parameters: ["page": page,"uid":UserModel.shareUser.data?.uid ?? String()])
            .responseString { response in
                
                self.withdrawHistroyTableview?.cr.endHeaderRefresh()
                self.withdrawHistroyTableview?.cr.endLoadingMore()
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<WithdrawModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        if page == 1 {
                            self.withdraws?.removeAll()
                            self.withdraws = basemodel.data?.data
                        }else {
                            if ((basemodel.data?.data?.count)! > 0) {
                                self.withdraws! += (basemodel.data?.data)!
                            }else {
                                return
                            }
                        }
                        self.withdrawHistroyTableview.reloadData()
                    case false:
                        MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
                
                
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

extension WithdrawHistoryViewController: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.withdraws?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! WithdrawHistoryTableViewCell
        cell.selectionStyle = .none
        let model = self.withdraws![indexPath.item]
        cell.withdrawMoneyLabel.text = "提现金额:" + model.arrival_price + "元"
        cell.bankNumLabel.text = "银行卡号:" + model.cardno
        cell.withdrawTimeLabel.text = "提现时间:" + model.dateline
        cell.remrakLabel.text = "备注:" + model.remarks
        switch model.status {
        case "0":
           cell.withdrawStatusLabel.text = "申请中"
            cell.withdrawStatusLabel.textColor = UIColor.HexColor(0x727272)
        case "1":
           cell.withdrawStatusLabel.textColor = BaseColor.NavColor
           cell.withdrawStatusLabel.text = "提现成功"
        case "2":
           cell.withdrawStatusLabel.textColor = UIColor.red
           cell.withdrawStatusLabel.text = "提现失败"
        default:
            break
        }

        return cell
        
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return ScaleHeight(120)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    //    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //        return 10
    //    }
    
}
